@gradingform @gradingform_metaagm
Feature: Stundent's submissions can be graded using Meta grading form.
  In order to grade a submission
  As a teacher
  I need to proceed to grading page and fill out the form (with Meta form set up).

  Background:
    Given the following "users" exist:
      | username | firstname | lastname | email            |
      | teacher1 | Teacher   | 1        | teacher1@asd.com |
      | student1 | Student   | 1        | student1@asd.com |
      | student2 | Student   | 2        | student2@asd.com |
    And the following "courses" exist:
      | fullname | shortname | format |
      | Course 1 | C1        | topics |
    And the following "course enrolments" exist:
      | user     | course | role           |
      | teacher1 | C1     | editingteacher |
      | student1 | C1     | student        |
      | student2 | C1     | student        |
    And I log in as "teacher1"
    And I follow "Course 1"
    And I turn editing mode on
    And I add a "Assignment" to section "1" and I fill the form with:
      | Assignment name | Test assignment 1 name      |
      | Description     | Test assignment description |
      | Grading method  | Meta AGM                    |

  Scenario: Using simple direct grading if Meta form isn't ready
    When I go to "Test assignment 1 name" advanced grading definition page
    Then I should see "No metacriteria defined yet"
    When I follow "View/grade all submissions"
    And I click on "Grade Student 1" "link" in the "Student 1" "table_row"
    Then I should see "Grade out of 100"

  Scenario: Grading two students
    # Create an assignemnt.
    Given I go to "Test assignment 1 name" advanced grading definition page
    And I create the metacriterion "Test metacriterion 1" with grade weight "1.0" and grading method "Marking guide"
    And I create the metacriterion "Test metacriterion 2" with grade weight "2.0" and grading method "Rubric"
    # Define Marking Guide
    And I create a definition for metacriterion "Test metacriterion 1"
    And I set the following fields to these values:
      | Name        | Metacriterion 1 guide  |
      | Description | Guide test description |
    And I define the following marking guide:
      | Guide Criterion 1 | For Students 1... | For Markers 1... | 30 |
      | Guide Criterion 2 | For Students 2... | For Markers 2... | 70 |
    And I press "Save marking guide and make it ready"
    # Define Rubric
    And I create a definition for metacriterion "Test metacriterion 2"
    And I set the following fields to these values:
      | Name        | Metacriterion 2 rubric  |
      | Description | Rubric test description |
    And I define the following rubric:
      | Rubric Criterion 1 | Level 11 |  1 | Level 12 | 20 | Level 13 | 40 | Level 14 | 50 |
      | Rubric Criterion 2 | Level 21 | 10 | Level 22 | 20 | Level 23 | 30 |
      | Rubric Criterion 3 | Level 31 |  5 | Level 32 | 20 |
    And I press "Save rubric and make it ready"
    # Grade Student 1
    When I go to "Student 1" "Test assignment 1 name" activity advanced grading page
    And I grade by filling the marking guide with:
      | Guide Criterion 1 |  1 | Very bad  |
      | Guide Criterion 2 | 60 | Very good |
    And I grade by filling the rubric with:
      | Rubric Criterion 1 | 50 | Very good                  |
      | Rubric Criterion 2 | 10 | Mmmm, you can do it better |
      | Rubric Criterion 3 | 5  | Not good                   |
    And I complete the advanced grading form with these values:
      | Feedback comments | In general... work harder... |
    # Grade student 2
    And I go to "Student 2" "Test assignment 1 name" activity advanced grading page
    And I grade by filling the marking guide with:
      | Guide Criterion 1 | 30 | Too good   |
      | Guide Criterion 2 | 70 | Brilliant! |
    And I grade by filling the rubric with:
      | Rubric Criterion 1 | 40 | Almost good     |
      | Rubric Criterion 2 | 30 | Very, very good |
      | Rubric Criterion 3 | 20 | Excellent       |
    And I complete the advanced grading form with these values:
      | Feedback comments | In general... work worse... |
    # Check Assign Submissions Table
    Then I should see "59.2" in the "Student 1" "table_row"
    And I should see "92.0" in the "Student 2" "table_row"

  Scenario: Regrading student if form has changed
    Given I go to "Test assignment 1 name" advanced grading definition page
    And I create the metacriterion "Test metacriterion 1" with grade weight "1.0" and grading method "Rubric"
    And I create a definition for metacriterion "Test metacriterion 1"
    And I set the following fields to these values:
      | Name        | Metacriterion 2 rubric  |
      | Description | Rubric test description |
    And I define the following rubric:
      | Rubric Criterion 1 | Level 11 |  1 | Level 12 | 20 | Level 13 | 40 | Level 14 | 50 |
      | Rubric Criterion 2 | Level 21 | 10 | Level 22 | 20 | Level 23 | 30 |
      | Rubric Criterion 3 | Level 31 |  5 | Level 32 | 20 |
    And I press "Save rubric and make it ready"
    And I go to "Student 1" "Test assignment 1 name" activity advanced grading page
    And I grade by filling the rubric with:
      | Rubric Criterion 1 | 50 | Very good                  |
      | Rubric Criterion 2 | 10 | Mmmm, you can do it better |
      | Rubric Criterion 3 | 5  | Not good                   |
    And I complete the advanced grading form with these values:
      | Feedback comments | In general... work harder... |
    And I should see "58.3" in the "Student 1" "table_row"
    When I go to "Test assignment 1 name" advanced grading definition page
    And I edit a definition for metacriterion "Test metacriterion 1"
    And I define the following rubric:
      | Rubric Criterion 2 | Level 21 | 15 | Level 22 | 40 | Level 23 | 60 |
      | Rubric Criterion 3 | Level 31 | 10 | Level 32 | 40 |
    And I press "Save"
    And I should see "You are about to save significant changes to a rubric that has already been used for grading"
    And I press "Continue"
    And I go to "Student 1" "Test assignment 1 name" activity advanced grading page
    Then I should see "definition was changed after this student had been graded"
    And I grade by filling the rubric with:
      | Rubric Criterion 2 | 40 | Mmmm, you can do it better |
      | Rubric Criterion 3 | 10 | Not good                   |
    And I complete the advanced grading form with these values:
      | Feedback comments | In general... START WORKING!!! |
    And I should see "33.3" in the "Student 1" "table_row"